package com.csxh.web.model.mybatis;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public abstract class Condition {

	public enum OrderMode {
		ASC, DESC
	}

	public enum LogicOp {
		AND, // (exp1) AND (exp2)
		OR, // (exp1) OR (exp2)
		NOT// NOT (exp1)
	}

	public enum RelationOp {
		EQ, // field = ?
		LG, // field > ?
		LT, // field < ?
		LGEQ, // field >= ?
		LTEQ, // field <= ?
		LIKE, // field LIKE %?%
		NOT_LIKE, // field NOT LIKE %?%
		IN, // field IN(?,?,?)
		NOT_IN, // field NOT IN(?,?,?)
		BETWEEN// field BETWEEN ? AND ?
	}

	private String[] fieldNames;
	private Map<String,String> field2AliasMap;
	private String[] fieldValues;
	private RelationOp relationOp = RelationOp.EQ;
	private LogicOp logicOp = LogicOp.AND;

	private String leftVoidCharator="`";
	private String rightVoidCharator="`";
	public Condition() {
		
	}
	
	public Condition(String leftVoidCharator, String rightVoidCharator) {
		super();
		this.leftVoidCharator = leftVoidCharator;
		this.rightVoidCharator = rightVoidCharator;
	}

	
	public void setFieldNames(String... fieldNames) {
		
		this.fieldNames =new String[]{};
		this.field2AliasMap=new HashMap<String,String>();
		for(String field:fieldNames){
			//判断是否有逗号分隔的字段列表:f1,f2,f3
			String[] ss=field.split(",");
			
			for(String s:ss){
			    
				int len=this.fieldNames.length;
				this.fieldNames=Arrays.copyOf(this.fieldNames, len+1);
				
				s=s.trim();
				//判断是否有别名:f1 as xx 或者 f1 xx
				String tt[]=s.split("\\s+(as|AS|As|aS)*\\s+");
				if(tt.length==2){
				    s=tt[0].trim();
				    this.field2AliasMap.put(s, tt[1].trim());		    
				}
				this.fieldNames[len]=s;
				
			}
		}
	}

	public String getSelectFields(){
		if(null==this.fieldNames || this.fieldNames.length==0){
			return "*";
		}
		StringBuilder sb=new StringBuilder();
		for(String field : this.fieldNames){
			String alias=this.field2AliasMap.get(field);
			if(null!=alias){				
				sb.append(this.leftVoidCharator).append(field).append(this.rightVoidCharator).append(" AS ").append(alias).append(",");
			}else{
				sb.append(this.leftVoidCharator).append(field).append(this.rightVoidCharator).append(",");				
			}
		}
		
		sb=sb.deleteCharAt(sb.length()-1);
		
		return sb.toString();
		
	}
	
	public abstract String getTableName();

	public String getTable(){
		return this.leftVoidCharator+getTableName()+this.rightVoidCharator;
	}


	private String where;
	public void setWhere(String where) {
		this.where = where;
	}
	public void setWhere(String where,RelationOp op,Object value) {
		switch(op){
		case EQ:
			if(value instanceof java.lang.String){				
				this.where = where+"= '"+value.toString() + "'";	
			}else{
				this.where = where+"= "+value.toString();					
			}
			break;
		default:

			this.where = where;
			
			break;
		}
		
		
	}
	public String getWhere() {
		return where;
	}
	
	private Object idValue;

	public void setIdValue(Object idValue) {
		this.idValue = idValue;
	}

	public Object getIdValue() {
		return idValue;
	}

	private String orderField;
	private OrderMode orderMode = OrderMode.DESC;

	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	public void setOrderMode(OrderMode orderMode) {
		this.orderMode = orderMode;
	}

	public String getOrderField() {
		return orderField;
	}

	public String getOrderMode() {
		return orderMode.toString();
	}

	private int offset=0;
	private int limit=Integer.MAX_VALUE;
	
	public int getOffset() {
		return offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}


}